import { createRouter, createWebHistory } from 'vue-router'
import haveToken from '@/utils/token/token.js'
import { get_user_info } from '@/api/user/user.js'
import index from '@/views/MyIndex.vue'

// 底部导航栏
import videoindex from '@/views/index/VideoIndex.vue'
import actionindex from '@/views/index/ActionIndex.vue'
import shopindex from '@/views/index/ShopIndex.vue'
import myindex from '@/views/index/MyIndex.vue'

import MyRecpmmend from '@/views/video/MyRecpmmend.vue'
import MyHot from '@/views/video/MyHot.vue'
import MyCartoon from '@/views/video/MyCartoon.vue'
import MyLive from '@/views/video/MyLive.vue'
import MyParty from '@/views/video/MyParty.vue'
import MyMovie from '@/views/video/MyMovie.vue'

import classify from '@/views/classify/ClassifyIndex.vue'
import search from '@/views/search/SearchIndex.vue'
import searchvideo from '@/views/search/SearchVideos.vue'
import login from '@/views/login/LoginIndex.vue'
import addlogin from '@/views/login/AddLogin.vue'
import user from '@/views/user/UserIndex.vue'
import changeimformation from '@/views/user/ChangeImformation.vue'
import upload from '@/views/upload/UpLoad.vue'

import useradd from '@/views/user/UserAdd.vue'
import userdetail from '@/views/user/UserDetail.vue'
import userstar from '@/views/user/UserStar.vue'
import usergood from '@/views/user/UserGood.vue'

import videodetail from '@/views/detail/VideoDetail.vue'
import friend from '@/views/friend/MyFriend.vue'
import personal from '@/views/user/PersonalIndex.vue'
import otherfriend from '@/views/friend/otherFriend.vue'

import chat from '@/views/chat/MyChat.vue'
import chatList from '@/views/chat/ChatList.vue'

import empty from '@/components/IsEmpty.vue/'
import feedback from '@/views/FeedBack/FeedBack.vue'

import setting from '@/views/setting/SettingMenu.vue'
import history from '@/views/history/MyHistory.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/index',
      component: index,
      children: [
        {
          path: '/index',
          redirect: '/myrecpmmend',
          component: videoindex,
          children: [
            {
              path: '/myrecpmmend',
              component: MyRecpmmend
            },
            {
              path: '/hot',
              component: MyHot
            },
            {
              path: '/movie',
              component: MyMovie
            },
            {
              path: '/Party',
              component: MyParty
            },
            {
              path: '/cartoon',
              component: MyCartoon
            }, {
              path: '/live',
              component: MyLive
            },
          ]
        },
        {
          path: '/action',
          component: actionindex,
        }, {
          path: '/shop',
          component: shopindex,
        }, {
          path: '/my',
          component: myindex,
        },
      ]
    },
    {
      path: '/classify',
      component: classify
    },
    {
      path: '/search',
      component: search
    },
    {
      path: '/search_video',
      component: searchvideo
    },
    {
      path: '/login',
      component: login
    },
    {
      path: '/addlogin',
      component: addlogin
    },
    {
      path: '/user',
      component: user,
      redirect: '/user_add',
      children: [
        {
          path: '/user_add',
          component: useradd
        },
        {
          path: '/user_detail',
          component: userdetail
        },
        {
          path: '/user_star',
          component: userstar
        }, {
          path: '/user_good',
          component: usergood
        }

      ]
    },
    {
      path: '/changeimformation',
      component: changeimformation
    },
    {
      path: '/upload',
      component: upload
    },
    {
      path: '/videodetail',
      component: videodetail
    },
    {
      path: '/friend',
      component: friend
    },
    {
      path: '/personal',
      component: personal,
      redirect: '/personal_add',
      children: [
        {
          path: '/personal_add',
          component: useradd
        },
        {
          path: '/personal_detail',
          component: userdetail
        },
        {
          path: '/personal_star',
          component: userstar
        }, {
          path: '/personal_good',
          component: usergood
        }
      ]
    },
    {
      path: '/otherfriend',
      component: otherfriend
    },
    {
      path: '/chat',
      component: chat
    },
    {
      path: '/chatList',
      component:chatList
    },
    {
      path: '/empty',
      component: empty
    },
    {
      path: '/feedback',
      component: feedback
    },
    {
      path: '/setting',
      component: setting
    },
    {
      path: '/history',
      component: history
    }

  ]
})

const routes = ['/user', '/upload', '/chat', '/chatList', '/user', '/user_star', '/user_good', '/user_add', '/changeimformation', '/history']
router.beforeEach((to, from, next) => {
  // 判断有没有登录
  if (!haveToken()) {
    if (routes.includes(to.path)) {
      next('/login')
    } else {
      next()
    }
  } else {
    if (routes.includes(to.path)) {
      get_user_info(haveToken()).then((res) => {
        if (res.data.status === 1) {
          alert('您的身份验证已过期,请重新登录')
          next('/login')
        }
        next()
      })
    } else {
      next();
    }

  }
})
export default router
